// miniprogram/pages/org/applyDetail/index.js
import { PosterCanvas } from "../../../utils/utils";
import { baseUrl } from "../../../http/config";
import {OrgModel} from '../../../model/org'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:-1,
    certUrl:baseUrl + "/static/2.jpg",
    type: 1,
    list: [],
    list2:[],
    canvasStatus: false,
    steps: [
      {
        text: '微心愿已被认领',
        desc: '',
      },
      {
        text: '微心愿审核中',
        desc: '',
      },
      {
        text: '微心愿审核结束',
        desc: '',
      },
      {
        text: '微心愿礼品已收到等待发往目的地',
        desc: '',
      },
      {
        text: '微心愿礼品运输中',
        desc: '',
      },
      {
        text: '微心愿礼品已寄到，荣誉证书制作中',
        desc: '',
      },
      {
        text: '荣誉证书制作完成发送至用户个人中心',
        desc: '',
      },
    ],
    active:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 已认领
    this.loadWishList1(1)
    // 已完成

    this.loadWishList2(2)
  },
  loadWishList1(bool){
    OrgModel.wishList(bool).then((res) => {
      if(res && res.code == 200) {
        console.log(res)
        this.setData({
          list:res.data
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  loadWishList2(bool){
    OrgModel.wishList(bool).then((res) => {
      if(res && res.code == 200) {
        console.log(res)
        this.setData({
          list2:res.data
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  typeTap(e) {
    console.log(e)
    this.setData({
      type: e.currentTarget.dataset.type
    })
  },
  // todo 
  showProcess(e) {
    // console.log(e)
    let wishId = e.currentTarget.dataset.id
    this.setData({
      currentIndex: wishId == this.data.currentIndex ? -1 : wishId
    })
  },
  loadProcess(id){
    OrgModel.wishProcess(id).then((res) => {
      if(res && res.code == 200) {
        console.log(res)
        this.setData({
          active:res.data.length-1
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  showCertificate() {
    this.setData({
      canvasStatus: true
    })
    let textobj = {
      name:wx.getStorageSync('user').name
    };
    let url = this.data.certUrl;
    let that = this
    PosterCanvas(url, textobj, function (tmpPath) {
      that.setData({
        savePic: tmpPath
      })
      that.getOriginImageToBase64(tmpPath)
    })
  },
  onClose(){
    this.setData({
      canvasStatus: false
    })
  },
  getOriginImageToBase64(originPicPath) {
    wx.getFileSystemManager().readFile({
      filePath: originPicPath, // 选择图片返回的相对路径
      encoding: 'base64', // 编码格式
      success: res => {
        res.data = "data:image/png;base64,"+res.data
        this.saveCert(res.data)
      }
    })
  },
  saveCert(){},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})